#include <iostream>
#include <unordered_map>
#include<unordered_set>
// TIP 要<b>Run</b>代码，请按 <shortcut actionId="Run"/> 或点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标
using namespace std;

class Solution {
    const double EPS = 1e-6;
    unordered_set<string> vis; // 记忆化：序列化后的状态

    string keyOf(vector<double> v) {
        sort(v.begin(), v.end());
        string k;
        k.reserve(v.size() * 12);
        for (double x : v) {
            // 保留6位，减少等价状态的浮点抖动
            long long fx = llround(x * 1e6);
            k += to_string(fx) + ",";
        }
        return k;
    }

    bool dfs(vector<double>& nums, double target) {
        int n = nums.size();
        if (n == 1) return fabs(nums[0] - target) < EPS;

        string k = keyOf(nums);
        if (vis.count(k)) return false;  // 该状态已失败过
        vis.insert(k);

        // 只取 i<j，避免重复
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                double a = nums[i], b = nums[j];
                vector<double> rest;
                rest.reserve(n - 1);
                for (int t = 0; t < n; ++t) if (t != i && t != j) rest.push_back(nums[t]);

                // 可交换运算：+  *
                {
                    rest.push_back(a + b);
                    if (dfs(rest, target)) return true;
                    rest.back() = a * b;
                    if (dfs(rest, target)) return true;
                    rest.pop_back();
                }
                // 非可交换运算：-  /
                {
                    rest.push_back(a - b);
                    if (dfs(rest, target)) return true;
                    rest.back() = b - a;
                    if (dfs(rest, target)) return true;
                    rest.pop_back();

                    if (fabs(b) > EPS) {
                        rest.push_back(a / b);
                        if (dfs(rest, target)) return true;
                        rest.pop_back();
                    }
                    if (fabs(a) > EPS) {
                        rest.push_back(b / a);
                        if (dfs(rest, target)) return true;
                        rest.pop_back();
                    }
                }
            }
        }
        return false;
    }

public:
    bool judgePoints(vector<int>& cards, int points) {
        vis.clear();
        vector<double> nums(cards.begin(), cards.end());
        return dfs(nums, points);
    }
};
class Solution1 {
public:
    bool judgePoints(vector<int>& cards, int points) {
        nums.assign(cards.begin(), cards.end());
        vis.clear();
        return dfs(nums, points);
    }

private:
    const double EPS = 1e-6;
    vector<double> nums;
    unordered_set<string> vis; // 记忆化：失败过的状态

    // 把当前数组做成 key（排序 + 保留6位）
    string makeKey(const vector<double>& v) {
        vector<long long> t; t.reserve(v.size());
        for (double x : v) t.push_back(llround(x * 1e6));
        sort(t.begin(), t.end());
        string k; k.reserve(t.size() * 12);
        for (auto z : t) { k += to_string(z); k += ','; }
        return k;
    }

    bool dfs(vector<double> nums, int target) {
        int n = nums.size();
        if (n == 1) return fabs(nums[0] - target) < EPS;

        // 记忆化剪枝（失败态）
        string key = makeKey(nums);
        if (vis.count(key)) return false;
        vis.insert(key);

        // 只枚举 i<j，避免镜像重复
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                vector<double> next;
                next.reserve(n - 1);
                for (int k = 0; k < n; ++k) if (k != i && k != j) next.push_back(nums[k]);

                // 层内去重：同一对(i,j)产生的等价结果只试一次
                unordered_set<long long> seen; seen.reserve(16);

                auto try_push = [&](double v)->bool{
                    long long q = llround(v * 1e6);
                    if (seen.insert(q).second) {
                        next.push_back(v);
                        if (dfs(next, target)) return true;
                        next.pop_back();
                    }
                    return false;
                };

                double a = nums[i], b = nums[j];
                // 可交换运算：+、*
                if (try_push(a + b)) return true;
                if (try_push(a * b)) return true;
                // 不可交换运算：-、/
                if (try_push(a - b)) return true;
                if (try_push(b - a)) return true;
                if (fabs(b) > EPS && try_push(a / b)) return true;
                if (fabs(a) > EPS && try_push(b / a)) return true;
            }
        }
        return false;
    }

    // 你原来的 compute 可保留或删除（已内联到上面）
    vector<double> compute(double a, double b) { /* 不再需要 */ return {}; }
};



class Solution2 {
public:
    ListNode* rotateList(ListNode* head, int k) {
        if (!head || !head->next || k == 0) return head;

        // 1. 计算链表长度，并找到尾节点
        int n = 1;
        ListNode* tail = head;
        while (tail->next) {
            tail = tail->next;
            n++;
        }

        // 2. 取余数
        k = k % n;
        if (k == 0) return head;

        // 3. 找到新的尾节点
        int steps = n - k - 1;
        ListNode* newTail = head;
        for (int i = 0; i < steps; i++) {
            newTail = newTail->next;
        }

        // 4. 重新链接
        ListNode* newHead = newTail->next;
        newTail->next = nullptr;
        tail->next = head;

        return newHead;
    }
};